/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.object;

import net.worlds.WorldsException;
import net.worlds.PropChangeHandler;
import net.worlds.object.GameObject;
import net.worlds.object.Part;
import org.jbox2d.dynamics.Body;


public class FixedRotation extends GameObject{
    public FixedRotation() throws WorldsException
    {
        super("FixedRotation");
        addPropChangeHandler("Parent", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                onParentChanged((GameObject) oldValue, (GameObject) value);
                if(value instanceof Part)
                {
                    if(oldValue instanceof Part)
                        ((Part)oldValue).setFixedRotation(false);
                    ((Part)value).setFixedRotation(true);
                }
                else if(value == null && oldValue instanceof Part)
                    destructor((Part) oldValue);
            }
        });
        addPropChangeHandler("Disabled", new PropChangeHandler()
        {
            @Override
            public void handle(Object oldValue, Object value) throws WorldsException {
                Boolean v = (Boolean) value;
                disabled = v;
                if(getParent() != null && getParent() instanceof Part)
                {
                    ((Part)getParent()).setFixedRotation(!v);
                }
            }
        });
        //x_;
    }
    public void destructor(Part oldParent) throws WorldsException
    {
        if(oldParent instanceof Part)
        {
            ((Part)oldParent).setFixedRotation(false);
            if(((Part)oldParent).getBody() != null && ((Part)oldParent).getBody().m_numFixedR == 0 && oldParent.getParent() instanceof AbstractCharacter)
            {
                ((AbstractCharacter)oldParent.getParent()).fixedRotationRemoved();
            }
        }
    }
    @Override
    public void addToWorld() throws WorldsException
    {
        super.addToWorld();
        if(getParent() instanceof Part)
        {
            ((Part)getParent()).setFixedRotation(true);
        }
    }
}
